<?php
/**
 * putObject，最大可以上传5G的文件。而使用 MultipartUploader 最大可以上传5TB的文件。
 * 当不知道使用哪个方法时，可以直接使用ObjectUploader 对象。它会自动判断
 * author: JiangJun
 * CreateTime:2021/8/30 6:03 下午
 */

use Aws\Exception\MultipartUploadException;
use Aws\S3\MultipartUploader;
use Aws\S3\ObjectUploader;
use Aws\S3\S3Client;

require '../sqs/vendor/autoload.php';

$s3Client = new S3Client([
    'region' => 'ap-northeast-2',
    'version' => '2006-03-01'
]);


$bucket = 'jiang-bucket';
$key = 'test.zip';

//注意这里的打开的一个文件流，而不是传入的路径.rb代表打开一个二进制文件
$source = fopen('./test.zip', 'rb');

$uploader = new ObjectUploader(
    $s3Client,
    $bucket,
    $key,
    $source
);

do {
    try {
        $result = $uploader->upload();
        if ($result["@metadata"]["statusCode"] == '200') {
            print('<p>File successfully uploaded to ' . $result["ObjectURL"] . '.</p>');
        }
        print($result);
    } catch (MultipartUploadException $e) {
        rewind($source);
        $uploader = new MultipartUploader($s3Client, $source, [
            'state' => $e->getState(),
        ]);
    }
} while (!isset($result));

fclose($source);